package main

import (
	"fmt"

	"gitee.com/cjianwen/go/socks"
)

type iTest interface {
	test()
	doTest()
}

type struct1 struct {
}

func (s *struct1) test() {
	fmt.Println("struct1.test")
}

func (s *struct1) doTest() {
	s.test()
}

type struct2 struct {
	*struct1
}

func (s *struct2) test() {
	fmt.Println("struct2.test")
}

func test_code() {
	s := &struct2{}
	s.doTest()

	var i iTest = s

	i.doTest()
	i.test()

	d := []byte{1, 2, 3, 4, 5, 6, 7}
	fmt.Println(d)
	copy(d, d[3:])
	fmt.Println(d)

	c := make(chan int, 1)
	c <- 1
	// c <- 2
	// c <- 3

	// _, o := <-c
	// fmt.Println("o = ", o)

	// for v := range c {
	// 	fmt.Println("v = ", v)
	// }

	// close(c)

	select {
	case c <- 666:
		fmt.Println("666")
	default:
		fmt.Println("default")
	}

	v, ok := <-c
	fmt.Println("v = ", v, ok)
}

type msgHandler struct{}

func (h *msgHandler) OnConnect(sock socks.SocketConnection) {
	fmt.Println("connect ", sock.RemoteAddr().String())
}

func (h *msgHandler) OnDisconnect(sock socks.SocketConnection) {
	fmt.Println("disconnect ", sock.RemoteAddr().String())
}

func (h *msgHandler) OnMessage(sock socks.SocketConnection, msg *socks.PackData) {
	fmt.Println("on message ", string(msg.Body), len(msg.Body))
}

// 定义回调接口
type ICallback interface {
	OnCallback(string)
}

type FunCallback func(string) // 定义回调函数的类型

// 给回调函数类型实现接口
func (f FunCallback) OnCallback(str string) {
	f(str)
}

// 返回回调函数，类型很重要
func getCalback() FunCallback {
	return func(s string) {
		fmt.Println(s)
	}
}

type MyStruct struct {
	IC ICallback // 持有回调接口
}

func main() {
	s := &MyStruct{IC: getCalback()}
	s.IC.OnCallback("1111")

	// test_code()

	// svr := socks.NewSocketServer(socks.SockOptions{HeadHandler: socks.NewDefaultHeadHandler(nil), MsgHandler: &msgHandler{}})
	// svr.Open(8008)

	// fmt.Println("server opended: 8008")
	// for {
	// 	time.Sleep(time.Second)
	// }

	arr := make([]byte, 3)
	arr = append(arr, 1)
	fmt.Println(arr)

	for i, v := range arr {
		fmt.Println(i, "=", v)
	}

	var t msgHandler
	fmt.Println(t)

	bits := []byte{1, 2, 3, 4, 5, 6}
	fmt.Println(bits)

	copy(bits[2:], bits[:])
	fmt.Println(bits)

	bits = []byte{1, 2, 3, 4, 5, 6}
	fmt.Println(bits)

	copy(bits[:], bits[2:])
	fmt.Println(bits)

}
